<template>
  <div>
    <el-card class="!border-none mb-4" shadow="never">
      <el-form class="mb-[-16px]" :model="queryParams" inline>

        <el-form-item label="时间(年月)" prop="bid">
          <el-select class="w-[280px]" v-model="queryParams.search_month" clearable placeholder="请选择时间(年月)">
            <el-option label="全部" value=""></el-option>
            <el-option
                v-for="(item, index) in searchLists.month_list"
                :key="index"
                :label="item.name"
                :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="院区/楼栋">
          <el-cascader
              :options="searchLists.cb_lists"
              clearable
              v-model="queryParams.cb_ids"
              filterable
              :props="props1"
          />
        </el-form-item>

        <el-form-item label="房间名" prop="room_keyword">
          <el-input class="w-[280px]" v-model="queryParams.room_keyword" clearable placeholder="请输入房间名"/>
        </el-form-item>

        <el-form-item label="设备信息" prop="device_keyword">
          <el-input class="w-[280px]" v-model="queryParams.device_keyword" clearable
                    placeholder="请输入设备名/设备编号"/>
        </el-form-item>

        <el-form-item label="记录时间" prop="log_time">
          <daterange-picker
              v-model:startTime="queryParams.start_time"
              v-model:endTime="queryParams.end_time"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
          <el-button type="warning" @click="handlePrint">打印</el-button>
          <export-data
              class="ml-2.5"
              :fetch-fun="apiRoomApartmentExportLists"
              :params="queryParams"
              :page-size="pager.size"/>

        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none" v-loading="pager.loading" shadow="never">
      <!--            <el-button v-perms="['room.room_apartment_device_log/add']" type="primary" @click="handleAdd">-->
      <!--                <template #icon>-->
      <!--                    <icon name="el-icon-Plus" />-->
      <!--                </template>-->
      <!--                新增-->
      <!--            </el-button>-->
      <!--            <el-button-->
      <!--                v-perms="['room.room_apartment_device_log/delete']"-->
      <!--                :disabled="!selectData.length"-->
      <!--                @click="handleDelete(selectData)"-->
      <!--            >-->
      <!--                删除-->
      <!--            </el-button>-->
      <div class="mt-4">
        <el-table :data="pager.lists" @selection-change="handleSelectionChange">
          <!--          <el-table-column type="selection" width="55"/>-->
          <el-table-column label="序号" prop="sort" width="80" show-overflow-tooltip/>

          <el-table-column label="房间" prop="rid">
            <template #default="{ row }">
              {{ row.name }} <br/>
              <el-text v-if="row.campus">{{ row.campus.name }}-</el-text>
              <el-text v-if="row.building">-{{ row.building.name }}</el-text>
            </template>
          </el-table-column>

          <el-table-column label="住户信息" width="150">
            <template #default="{ row }">
              <!--title="Title"-->
              <el-popover placement="top-start" :width="350" trigger="hover">
                <template #reference>
                  <el-button type="info">
                    {{ row.user_count }}人
                  </el-button>
                </template>
                <el-table :data="row.user">
                  <el-table-column width="150" property="user_name" label="姓名"/>
                  <el-table-column width="150" property="user_mobile" label="手机号"/>
                </el-table>
              </el-popover>
            </template>
          </el-table-column>

          <!--          <el-table-column label="设备" prop="device_name">-->
          <!--            <template #default="{ row }">-->
          <!--              &lt;!&ndash;              <view v-if="row.device">&ndash;&gt;-->
          <!--              {{ row.device_name }} <br/>-->
          <!--              {{ row.device_no }} <br/>-->
          <!--              &lt;!&ndash;                {{ row.device_type }}&ndash;&gt;-->
          <!--              &lt;!&ndash;              </view>&ndash;&gt;-->
          <!--            </template>-->
          <!--          </el-table-column>-->

          <!--          <el-table-column label="底数" prop="base_number" show-overflow-tooltip/>-->
          <!--          <el-table-column label="读数" prop="reading" show-overflow-tooltip/>-->
          <!--          <el-table-column label="使用量" prop="use_total" show-overflow-tooltip/>-->

          <!--          <el-table-column label="设备系数" prop="coefficient" show-overflow-tooltip/>-->
          <!--          <el-table-column label="水电价" prop="price" show-overflow-tooltip/>-->

          <el-table-column label="总水用量" prop="room_s_use_total" show-overflow-tooltip/>
          <el-table-column label="均水价" prop="room_s_price" show-overflow-tooltip/>
          <el-table-column label="总水费用" prop="room_s_total_fee" show-overflow-tooltip/>

          <el-table-column label="总电用量" prop="room_d_use_total" show-overflow-tooltip/>
          <el-table-column label="均电价" prop="room_d_price" show-overflow-tooltip/>
          <el-table-column label="总电费用" prop="room_d_total_fee" show-overflow-tooltip/>

          <el-table-column label="电梯费" prop="room_lift_fee" show-overflow-tooltip/>
          <el-table-column label="二次压力费" prop="room_second_pressure_fee" show-overflow-tooltip/>

          <el-table-column label="总费用" prop="room_all_total_fee" show-overflow-tooltip/>
          <el-table-column label="记录时间" prop="log_date" show-overflow-tooltip/>

          <!--                    <el-table-column label="操作" width="120" fixed="right">-->
          <!--                        <template #default="{ row }">-->
          <!--                             <el-button-->
          <!--                                v-perms="['room.room_apartment_device_log/edit']"-->
          <!--                                type="primary"-->
          <!--                                link-->
          <!--                                @click="handleEdit(row)"-->
          <!--                            >-->
          <!--                                编辑-->
          <!--                            </el-button>-->
          <!--                            <el-button-->
          <!--                                v-perms="['room.room_apartment_device_log/delete']"-->
          <!--                                type="danger"-->
          <!--                                link-->
          <!--                                @click="handleDelete(row.id)"-->
          <!--                            >-->
          <!--                                删除-->
          <!--                            </el-button>-->
          <!--                        </template>-->
          <!--                    </el-table-column>-->
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists"/>
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false"/>

    <print-popup v-if="showPrint" ref="printRef" @close="showPrint = false"
                 :print-keyword="printLists.pkl_keyword"
                 :print-lists="printLists.pkl_lists"/>

  </div>
</template>

<script lang="ts" setup name="roomApartmentDeviceLogLists">
import {usePaging} from '@/hooks/usePaging'
import {useDictData} from '@/hooks/useDictOptions'
import {
  apiRoomApartmentDeviceLogLists,
  apiRoomApartmentDeviceLogDelete,
  apiRoomApartmentPrintListsDeviceLog,
  apiRoomApartmentExportLists
} from '@/api/room/room_apartment_device_log'
import {timeFormat} from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {apartmentSearchLists} from "@/api/room/room_apartment";
import {reactive} from "vue";
import {list} from "postcss";

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)

import PrintPopup from '@/views/system/print_page.vue'

const printRef = shallowRef<InstanceType<typeof PrintPopup>>()
const showPrint = ref(false)
const printLists = reactive({
  pkl_keyword: [],
  pkl_lists: [],
})
const handlePrint = async () => {
  const print = await apiRoomApartmentPrintListsDeviceLog(queryParams)
  printLists.pkl_keyword = print.keyword
  printLists.pkl_lists = print.lists
  showPrint.value = true
  await nextTick()
  printRef.value?.open()
}

const props1 = {
  checkStrictly: true
}

// 查询条件
const queryParams = reactive({
  cid: '',
  rid: '',
  device_id: '',
  coefficient: '',
  price: '',
  base_number: '',
  reading: '',
  use_total: '',
  lift_fee: '',
  second_pressure_fee: '',
  total_fee: '',
  log_time: '',
  start_time: '',
  end_time: '',
  cb_ids: '',
  room_keyword: '',
  device_keyword: '',
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({id}) => id)
}

// 获取字典数据
const {dictData} = useDictData('show_status')

const searchLists = reactive({
  campus_list: [],
  building_list: [],
  cb_lists: [],
  month_list: [],
})
const getSearchLists = async () => {
  const lists = await apartmentSearchLists([])
  searchLists.campus_list = lists.campus_list
  searchLists.building_list = lists.building_list
  searchLists.cb_lists = lists.cb_lists
  searchLists.month_list = lists.month_list
  return lists;
}
getSearchLists()


// 分页相关
const {pager, getLists, resetParams, resetPage} = usePaging({
  fetchFun: apiRoomApartmentDeviceLogLists,
  params: queryParams
})

// 添加
const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm('确定要删除？')
  await apiRoomApartmentDeviceLogDelete({id})
  getLists()
}

getLists()
</script>

